package com.hongning.railway.ui.adapter;

import android.databinding.ViewDataBinding;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.hongning.railway.databinding.ItemAnswerBinding;
import com.hongning.railway.ui.main.fragments.college.exam.ExamDetailFragmentViewModel;
import com.hongning.railway.ui.main.fragments.college.exam.itemviewmodel.ExamItemViewModel;
import com.hongning.railway.ui.widget.CheckBoxGroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;
import me.tatarka.bindingcollectionadapter2.BindingViewPagerAdapter;

/**
 * Author: zhangjing
 * DATA:2019-08-10 16:41
 * DESC:
 * email:zhangjingworkmol@163.com
 */
public class AnswerAdapter extends BindingRecyclerViewAdapter<ExamItemViewModel> {
    private RecyclerView recyclerView;
    private ExamDetailFragmentViewModel viewModel;

    public void setViewModel(ExamDetailFragmentViewModel viewModel) {
        this.viewModel = viewModel;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        //这样做貌似效率比较低
        holder.setIsRecyclable(false);
    }

    @Override
    public void onBindBinding(ViewDataBinding binding, int variableId, int layoutRes, int position, final ExamItemViewModel item) {
        super.onBindBinding(binding, variableId, layoutRes, position, item);
        ItemAnswerBinding answerBinding = (ItemAnswerBinding) binding;
        if (position == 0) {
            answerBinding.btnPre.setEnabled(false);
        } else {
            answerBinding.btnPre.setEnabled(true);
        }
        if (position == getItemCount() - 1) {
            answerBinding.btnNext.setEnabled(false);
        } else {
            answerBinding.btnNext.setEnabled(true);
        }
        answerBinding.btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = position + 1;
                recyclerView.smoothScrollToPosition(pos);
                viewModel.currentpos.set(pos + 1 + "/" + getItemCount() + "题");
            }
        });
        answerBinding.btnPre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = position - 1;
                recyclerView.smoothScrollToPosition(pos);
                viewModel.currentpos.set(pos + 1 + "/" + getItemCount() + "题");
            }
        });
        LinearLayout parent = answerBinding.llAnswer;
        HashMap<CheckBox, String> checkBoxMap = new HashMap<>();
        for (int i = 0; i < parent.getChildCount(); i++) {
            CheckBox checkBox = (CheckBox) parent.getChildAt(i);
            if (!item.getResOptionBean().getSelectAnswer().contains(getAnswer(i))){
                checkBox.setChecked(false);
            }
            checkBoxMap.put(checkBox, getAnswer(i));
        }
        CheckBoxGroup.CheckedChangeListener<String> checkedChangeListener1 = new CheckBoxGroup.CheckedChangeListener<String>() {
            @Override
            public void onCheckedChange(ArrayList<String> values) {
                item.getResOptionBean().setSelectAnswer(values);
            }
        };
        CheckBoxGroup<String> checkBoxGroup = new CheckBoxGroup<String>(checkBoxMap, checkedChangeListener1);
        checkBoxGroup.setSingle(item.isSingle());
    }

    public String getAnswer(int i) {
        switch (i) {
            case 0:
                return "A";
            case 1:
                return "B";
            case 2:
                return "C";
            case 3:
                return "D";
            case 4:
                return "E";
            case 5:
                return "F";
            default:
                return "";
        }
    }

    public int getPosition(String ans) {
        switch (ans) {
            case "A":
                return 0;
            case "B":
                return 1;
            case "C":
                return 2;
            case "D":
                return 3;
            case "E":
                return 4;
            case "F":
                return 5;
            default:
                return 0;
        }
    }

    @Override
    public void setItems(@Nullable List<ExamItemViewModel> items) {
        super.setItems(items);
        viewModel.currentpos.set(1 + "/" + items.size() + "题");
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }
}
